import { Controller } from "../../mvc/core/Controller";
import { MagicWeaponRankingWindow } from "../view/magicWeaponRanking/MagicWeaponRankingWindow";
import { MagicWeaponBtnIcon } from "../view/magicWeaponRanking/MagicWeaponBtnIcon";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SAmuletCrossToplistInfoSeq } from "../../../Message/Public/SAmuletCrossToplistInfoSeq";
import { ItemData } from "../resource/info/ItemData";
import { JSON } from "../../../com/mui/serialization/json/JSON";
import { MagicWeaponCellInfo } from "../view/magicWeaponRanking/MagicWeaponCellInfo";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { LoaderHelp } from "../../common/display/LoaderHelp";
import { ResFileConst } from "../resource/ResFileConst";
type int = number;
//class MagicWeaponRankingController
    
    export  class MagicWeaponRankingController extends Controller
    {
       

        public get magicWeaponRankingWindow(): MagicWeaponRankingWindow
        {
            if (this._magicWeaponWindow == null) 
            {
                this._magicWeaponWindow = new MagicWeaponRankingWindow();
            }
            return this._magicWeaponWindow;
        }

        public get magicWeaponBtnIcon(): MagicWeaponBtnIcon
        {
            if (this._magicWeaponBtnIcon == null) 
            {
                this._magicWeaponBtnIcon = new MagicWeaponBtnIcon();
            }
            return this._magicWeaponBtnIcon;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onAcitveTimeUpdateHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRankingWindowOpen, this.onWindowOpenHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRankingRewardGet, this.onMagicWeaponRankingRewardGetHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRankingRewardGetReq, this.onMagicWeaponRankingRewardGetReqHandler);
            Dispatcher.addEventListener(EventName.MagicWeaponRankingCurrentServer, this.getRankingData);
            Dispatcher.addEventListener(EventName.RankGetDataSuccess, this.onGetCurrentServerInfoSuccess);
            Dispatcher.addEventListener(EventName.MagicWeaponRankingCrossServer, this.getRankingData);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGetCrossAmuletInfoBack, this.onGetCrossInfoSuccess);
            return;
        }

        /* internal  */onGetCrossInfoSuccess(arg1: MessageBlock): void
        {
            var loc7=null;
            var loc8=0;
            var loc9=0;
            var loc10=null;
            var loc11=null;
            var loc12=null;
            var loc13=0;
            var loc14=0;
            var loc15=null;
            var loc1=arg1.messageBase as SAmuletCrossToplistInfoSeq;
            var loc2=loc1.getType();
            var loc3;
            var loc4=(loc3 = loc1.vAmuletCrossToplistInfo).length;
            var loc5=new Array<any>();
            var loc6=0;
            while (loc6 < loc4) 
            {
                loc8 = (loc7 = loc3[loc6]).score;
                loc9 = loc6 + 1;
                loc10 = new ItemData(loc7.itemCode);
                if (loc7.jsStr != null) 
                {
                    loc10.setItemExInfo(JSON.deserialize(loc7.jsStr));
                }
                loc11 = loc10.itemInfo.htmlName;
                loc12 = loc7.name;
                loc13 = loc7.proxyId;
                loc14 = loc7.serverId;
                loc15 = new MagicWeaponCellInfo(loc8, loc9, loc11, loc13, loc14, loc12, loc10, true);
                loc5.push(loc15);
                ++loc6;
            }
            cache.magicWeaponRankingCache.crossServerData = loc5;
            if (cache.magicWeaponRankingCache.crossServerRewardMap != null) 
            {
                if (this._magicWeaponWindow && !this._magicWeaponWindow.isHide) 
                {
                    this._magicWeaponWindow.updateData(true);
                }
            }
            else 
            {
                this.onMagicWeaponRankingRewardGetHandler(null, 1);
            }
            return;
        }

        /* internal  */onGetCurrentServerInfoSuccess(arg1: DataEvent): void
        {
            var loc10=null;
            var loc11=0;
            var loc12=0;
            var loc13=null;
            var loc14=null;
            var loc15=null;
            var loc16=0;
            var loc17=0;
            var loc18=null;
            var loc1=int(arg1.data);
            var loc2=cache.magicWeaponRankingCache.curServerMagicWeaponType;
            var loc3=cache.magicWeaponRankingCache.isActiveInEffect(false);
            var loc4=cache.magicWeaponRankingCache.isActiveInEffect(true);
            if (!(loc1 == loc2) || cache.ranking.isHaveList(loc2) == false || !loc3 && !loc4) 
            {
                return;
            }
            var loc5;
            var loc6=(loc5 = cache.ranking.getData(loc2)).length;
            var loc7=new Array<any>();
            var loc8=[];
            var loc9=0;
            while (loc9 < loc6) 
            {
                loc11 = (loc10 = loc5[loc9]).valueOne;
                loc12 = loc10.rank;
                loc14 = (loc13 = new ItemData(loc10.entityId)).itemInfo.htmlName;
                loc15 = loc10.ownerName;
                loc16 = loc10.proxyId;
                loc17 = loc10.serverId;
                loc8.push(loc10.entityUid);
                (loc18 = new MagicWeaponCellInfo(loc11, loc12, loc14, loc16, loc17, loc15, loc13, false)).itemdataUid = loc10.entityUid;
                loc7.push(loc18);
                ++loc9;
            }
            cache.magicWeaponRankingCache.curServerData = loc7;
            GameProxy.rankingProxy.getEquipScoreInfo(loc8, this.getUidMapComplete, 0);
            return;
        }

        /* internal  */getUidMapComplete(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            var loc4=null;
            var loc5=null;
            var loc1=cache.magicWeaponRankingCache.curServerData;
            var loc2=loc1.length;
            var loc3=0;
            while (loc3 < loc2) 
            {
                loc4 = loc1[loc3];
                if ((loc5 = arg1[loc4.itemdataUid]) && !(loc5.jsStr == null)) 
                {
                    loc4.itemdata.setItemExInfo(JSON.deserialize(loc5.jsStr));
                }
                ++loc3;
            }
            if (cache.magicWeaponRankingCache.currentServerRewardMap != null) 
            {
                if (this._magicWeaponWindow && !this._magicWeaponWindow.isHide) 
                {
                    this._magicWeaponWindow.updateData(false);
                }
            }
            else 
            {
                this.onMagicWeaponRankingRewardGetHandler(null, 0);
            }
            return;
        }

        /* internal  */onAcitveTimeUpdateHandler(arg1: DataEvent): void
        {
            var loc1=cache.magicWeaponRankingCache.canShowActive(false);
            var loc2=cache.magicWeaponRankingCache.canShowActive(true);
            var loc3=cache.magicWeaponRankingCache.isActiveInEffect(false);
            if (loc1 || loc2) 
            {
                this.showIcon();
            }
            else if (loc3) 
            {
                this.checkAndShowShowIcon(null);
            }
            else 
            {
                if (this.magicWeaponBtnIcon && !this.magicWeaponBtnIcon.isHide) 
                {
                    this.magicWeaponBtnIcon.hide();
                }
                if (this._magicWeaponWindow && this._magicWeaponWindow.isHide == false) 
                {
                    this._magicWeaponWindow.hide();
                }
            }
            return;
        }

        /* internal  */showIcon(): void
        {
            this.magicWeaponBtnIcon.show();
            if (this._magicWeaponWindow && this._magicWeaponWindow.isHide == false) 
            {
                this._magicWeaponWindow.updateView(false);
            }
            return;
        }

        /* internal  */checkAndShowShowIcon(arg1: Object /* flash.events.Event */): void
        {
            RolePlayer.instance.removeEventListener(PlayerEvent.RoleLevelUpdate, this.checkAndShowShowIcon);
            var loc1=cache.magicWeaponRankingCache.canShowActive(false);
            if (loc1) 
            {
                this.showIcon();
            }
            else 
            {
                RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.checkAndShowShowIcon);
            }
            return;
        }

        /* internal  */getRankingData(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.type == EventName.MagicWeaponRankingCrossServer;
            if (loc1) 
            {
                GameProxy.rankingProxy.getCrossMagicWeaponRankingData();
            }
            else 
            {
                GameProxy.rankingProxy.getRankingData(cache.magicWeaponRankingCache.curServerMagicWeaponType);
            }
            return;
        }

        /* internal  */onWindowOpenHandler(arg1: DataEvent): void
        {
            if (this.magicWeaponRankingWindow.isHide) 
            {
                if (this.magicWeaponRankingWindow.isInit) 
                {
                    this.magicWeaponRankingWindow.show();
                }
                else 
                {
                    LoaderHelp.addResCallBack(ResFileConst.magicWeaponRanking, this.loadSourceFinishAndToShow);
                }
            }
            else 
            {
                this.magicWeaponRankingWindow.hide();
            }
            return;
        }

        /* internal  */loadSourceFinishAndToShow(): void
        {
            this.magicWeaponRankingWindow.init();
            this.magicWeaponRankingWindow.show();
            return;
        }

        /* internal  */onMagicWeaponRankingRewardGetHandler(arg1: DataEvent, arg2: int=-1): void
        {
            if (arg1) 
            {
                arg2 = int(arg1.data);
            }
            GameProxy.daily.getMagicWeaponRankingReward(arg2);
            return;
        }

        /* internal  */onMagicWeaponRankingRewardGetReqHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.type != 0 ? true : false;
            cache.magicWeaponRankingCache.setMagicWeaponRankingReward(loc1.value, loc2);
            if (this._magicWeaponWindow && !this._magicWeaponWindow.isHide) 
            {
                this._magicWeaponWindow.updateReward(loc2);
                this._magicWeaponWindow.updateData(loc2);
            }
            return;
        }

        private /* var */_magicWeaponWindow: MagicWeaponRankingWindow;

        private /* var */_magicWeaponBtnIcon: MagicWeaponBtnIcon;
    }
